import type { ApiPagination, ApiPaginationParams } from '#types/utils'
import { formatDate } from '@vben/utils'
import { requestClient } from '../request'
import type { taskItemForm, taskPageListItem } from './model/taskModel'
// 商品任务管理
export default class TaskApi {
    static get inst() {
        return TaskApi._inst || (TaskApi._inst = new TaskApi())
    }
    private static _inst: TaskApi
    add(params: Partial<taskItemForm>) {
        delete params.divider
        if (params.periodOfValidity) {
            const [start, end] = params.periodOfValidity
            if (start !== undefined && end !== undefined) {
                params.startTime = String(formatDate(start))
                params.endTime = String(formatDate(end))
            }
        }
        delete params.periodOfValidity
        return requestClient.post<null>('/goodsTask/add', params)
    }
    delete(ids: Array<number | string>) {
        return requestClient.delete<null>(`/goodsTask/delete/${ids}`)
    }
    edit(id: number | string, params: Partial<taskItemForm>) {
        delete params.divider
        if (params.periodOfValidity) {
            const [start, end] = params.periodOfValidity
            if (start !== undefined && end !== undefined) {
                params.startTime = String(formatDate(start))
                params.endTime = String(formatDate(end))
            }
        }
        delete params.periodOfValidity
        return requestClient.put<null>(`/goodsTask/edit/${id}`, params)
    }
    /**
     * 管理分页
     * @param params
     */
    getPageList(params: ApiPaginationParams<{ username?: string }>) {
        return requestClient.get<ApiPagination<taskPageListItem>>('/goodsTask/index', {
            params
        })
    }
}
